/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelecionarCampeonato.java
 *
 * Created on 25/06/2011, 02:08:16
 */

package GUI.campeonato;

import bd.MySql;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import util.Datas;

/**
 *
 * @author Gabriel
 */
public class SelecionarCampeonato extends javax.swing.JDialog {

    /** Creates new form SelecionarCampeonato */
    public SelecionarCampeonato(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
    }

    // classe para exibir os dados da tabela campeonato_estadual na caixa de diálogo
    private class TableModelCampeonatos extends DefaultTableModel{
        public TableModelCampeonatos() {
            MySql conn = new MySql(GUI.Main.config);

            this.addColumn("Id");
            this.addColumn("Estado");
            this.addColumn("Data Início");

            // pega os dados da tabela do banco de dados
            ArrayList<ArrayList<String>> resultado = conn.getTable("SELECT idCampeonato, estado, dataInicio FROM campeonato_estadual");

            // adiciona na TableModel
            for(int i=0;i< resultado.size();i++) {
                String rowData[] = new String[resultado.get(i).size()];
                for(int j=0;j<resultado.get(0).size();j++) {
                    if(j != 2)
                        rowData[j] = resultado.get(i).get(j);
                    else {
                        Datas d = Datas.MySQLDateToJavaDate(resultado.get(i).get(j));
                        rowData[j] = d.getDay() + "/" + d.getMonth() + "/" + d.getYear();
                    }
                }
                this.addRow(rowData);
            }
        }
    }

    public int showDialog() {
        //this.setVisible(true);
        this.setVisible(true);
        int linha_selecionada = jTable1.getSelectedRow();

        if(linha_selecionada >= 0) {
            return Integer.valueOf(jTable1.getValueAt(linha_selecionada, 0).toString());
        } else {
            return -1;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new TableModelCampeonatos());
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText("Ok");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 175, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SelecionarCampeonato dialog = new SelecionarCampeonato(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
